home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / MAP Viewer / PTSLoader.cpp < prev    next >
C/C++ Source or Header  |  2003-10-09  |  3KB  |  96 lines

  1. /*
  2. Half-Life MAP viewing utility.
  3. Copyright (C) 2003  Ryan Samuel Gregg
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. */
  19.  
  20. #include "stdafx.h"
  21. #include "PTSLoader.h"
  22.  
  23. CPTSLoader::CPTSLoader(CConfig *Config, CRichTextBox *txtConsole)
  24. {
  25.     this->Config = Config;
  26.     this->txtConsole = txtConsole;
  27. }
  28.  
  29. bool CPTSLoader::LoadPTSFile(String *sFile, CPointFile **PointFile)
  30. {
  31.     StreamReader *PTSFile;
  32.     String *PTSData;
  33.     String *PTSLines[];
  34.     String *PTSLine[];
  35.     Vertex3f NewVertex;
  36.  
  37.     txtConsole->Print(String::Concat(S"Loading ", sFile, S"..."), Color::Green);
  38.  
  39.     try
  40.     {
  41.         PTSFile = File::OpenText(sFile);
  42.     }
  43.     catch(Exception *e)
  44.     {
  45.         txtConsole->Print(String::Concat(S"Error loading ", sFile, S": ", e->Message, S"\n"), Color::Red);
  46.         return false;
  47.     }
  48.  
  49.     try
  50.     {
  51.         PTSData = PTSFile->ReadToEnd();
  52.     }
  53.     catch(Exception *e)
  54.     {
  55.         txtConsole->Print(String::Concat(S"Error loading ", sFile, S": ", e->Message, S"\n"), Color::Red);
  56.         PTSFile->Close();
  57.         return false;
  58.     }
  59.  
  60.     PTSFile->Close();
  61.  
  62.     if(PTSData->EndsWith("\n"))
  63.         PTSData = PTSData->Remove(PTSData->Length - 1, 1);
  64.  
  65.     PTSLines = PTSData->Split(S"\n"->ToCharArray());
  66.  
  67.     *PointFile = new CPointFile(Config, PTSLines->Length);
  68.  
  69.     for(int i = 0; i < PTSLines->Length; i++)
  70.     {
  71.         PTSLine = PTSLines[i]->Split(S" "->ToCharArray());
  72.         
  73.         if(PTSLine->Length < 3)
  74.         {
  75.             txtConsole->Print(String::Concat(S"Error loading ", sFile, S": Too few points on line.\n"), Color::Red);
  76.             return false;
  77.         }
  78.  
  79.         try
  80.         {
  81.             NewVertex.X = Convert::ToSingle(PTSLine[0]);
  82.             NewVertex.Y = Convert::ToSingle(PTSLine[1]);
  83.             NewVertex.Z = Convert::ToSingle(PTSLine[2]);
  84.             (*PointFile)->SetVertex(i, NewVertex);
  85.         }
  86.         catch(Exception *e)
  87.         {
  88.             txtConsole->Print(String::Concat(S"Error loading ", sFile, S": Cannot convert string to float.\n"), Color::Red);
  89.             return false;
  90.         }
  91.     }
  92.  
  93.     txtConsole->Print(String::Concat(PTSLines->Length.ToString(), S" points loaded.\n"), Color::Green);
  94.  
  95.     return true;
  96. }